<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * author	:	Tran Van Thanh
 * email	:	thanhtran@vietnambiz.com
 * date		:	30/08/2011
 */

require_once 'client'.EXT;

class Stores extends Client {

	function __construct()
	{
		parent::__construct();
		$this->load->library('pagination');
		$this->load->model(array('home_model','stores_model'));
		$this->load->helper('custom_string');
		
		//Get company
		$this->data['company']	= $this->home_model->get_company();
		//Get Utilities
		$this->data['utilities'] = $this->home_model->get_utilities();
		//Get support
		$this->data['support'] 	= $this->home_model->get_support();
	}

	function index($page = 1)
	{		
		$limit		=	8;		
		$offset     = 	($page-1)*$limit;
				
		$result		=	$this->stores_model->get_all_products($limit,$offset);

		$num_rows   =	$result['num_rows'];
		$rows		=	$result['rows'];
		$total		=	ceil($num_rows/$limit);			
		
		$url		=	base_url().'gian-hang';		
			
		$conf = array(
			'cur_page'			=>	$page,
			'base_url'			=>	$url,
			'total_rows'		=>	$total,
			'cur_page'			=>	$page,			
			'cur_class'			=>	'currentpage',
			'prev_class'		=>	'prevnext',
			'next_class'		=>	'prevnext',
			'next_link'			=>	'Tiếp theo',
			'prev_link'			=>	'Trang trước',
			'show_total'		=>	'no',
			'show_first_last'	=>	'no'
		);
		
		$this->pagination->initialize($conf);
		$this->data['pagnav'] 		= $this->pagination->non_ajax();
		$this->data['page']			= $page;
		$this->data['num_rows'] 	= $num_rows;
		$this->data['total_page'] 	= $total;
		
		$this->data['all_products']	= $rows;
		
		//meta tag
	    $this->data['title'] 			= get_title('[TITLE]',"Gian hàng");
	    $this->data['description'] 		= get_description('[DESCRIPTION]',"Gian hàng");
		$this->data['keywords'] 		= get_keywords('[KEYWORDS]',"Gian hàng");	    
		//end meta tag
									
		$this->data['tpl_file']		=	'stores/index';
		$this->load->view('layout/default',$this->data);	
	}
	
	function filter_price($price = null,$page = 1)
	{		
		$limit		=	8;
		$offset     = 	($page-1)*$limit;
		if(isset($price) && $price != null)
		{
			$result		=	$this->stores_model->get_all_products_by_price($price,$limit,$offset);
		}
		$num_rows   =	$result['num_rows'];
		$rows		=	$result['rows'];
		$total		=	ceil($num_rows/$limit);			
		
		$menu 			=   trim($this->uri->segment(3));
		$menu_current 	=   str_replace('.html','',$menu);
		$url			=	base_url().'gian-hang/price_max/'.$menu_current;		
					
		$conf = array(
			'cur_page'			=>	$page,
			'base_url'			=>	$url,
			'total_rows'		=>	$total,
			'cur_page'			=>	$page,			
			'cur_class'			=>	'currentpage',
			'prev_class'		=>	'prevnext',
			'next_class'		=>	'prevnext',
			'next_link'			=>	'Tiếp theo',
			'prev_link'			=>	'Trang trước',
			'show_total'		=>	'no',
			'show_first_last'	=>	'no'
		);
		
		$this->pagination->initialize($conf);
		$this->data['pagnav'] 		= $this->pagination->non_ajax();
		$this->data['page']			= $page;
		$this->data['num_rows'] 	= $num_rows;
		$this->data['total_page'] 	= $total;
		
		$this->data['all_products']	= $rows;
		
		//meta tag
	    $this->data['title'] 			= get_title('[TITLE]',"Gian hàng");
	    $this->data['description'] 		= get_description('[DESCRIPTION]',"Gian hàng");
		$this->data['keywords'] 		= get_keywords('[KEYWORDS]',"Gian hàng");	    
		//end meta tag
									
		$this->data['tpl_file']		=	'stores/index';
		$this->load->view('layout/default',$this->data);
	}
	
	function filter_company($name_link = null,$page = 1)
	{
		$limit		=	8;
		$offset     = 	($page-1)*$limit;
		if(isset($name_link) && $name_link != null)
		{	
			$id_company = $this->stores_model->get_company_info_by_name_link($name_link);
			$result		=	$this->stores_model->get_all_products_by_company($id_company->id,$limit,$offset);
		}
		$num_rows   =	$result['num_rows'];
		$rows		=	$result['rows'];
		$total		=	ceil($num_rows/$limit);			
		
		$menu 			= trim($this->uri->segment(3));
		$menu_current 	= str_replace('.html','',$menu);
		
		$url			=	base_url().'gian-hang/company/'.$menu_current;		
			
		$conf = array(
			'cur_page'			=>	$page,
			'base_url'			=>	$url,
			'total_rows'		=>	$total,
			'cur_page'			=>	$page,			
			'cur_class'			=>	'currentpage',
			'prev_class'		=>	'prevnext',
			'next_class'		=>	'prevnext',
			'next_link'			=>	'Tiếp theo',
			'prev_link'			=>	'Trang trước',
			'show_total'		=>	'no',
			'show_first_last'	=>	'no'
		);
		
		$this->pagination->initialize($conf);
		$this->data['pagnav'] 		= $this->pagination->non_ajax();
		$this->data['page']			= $page;
		$this->data['num_rows'] 	= $num_rows;
		$this->data['total_page'] 	= $total;
		
		$this->data['all_products']	= $rows;
		
		//meta tag
	    $this->data['title'] 			= get_title('[TITLE]',"Gian hàng");
	    $this->data['description'] 		= get_description('[DESCRIPTION]',"Gian hàng");
		$this->data['keywords'] 		= get_keywords('[KEYWORDS]',"Gian hàng");	    
		//end meta tag
									
		$this->data['tpl_file']		=	'stores/index';
		$this->load->view('layout/default',$this->data);
	}
	
	function filter_utilities($name_link = null,$page = 1)
	{		
		$limit		=	8;
		$offset     = 	($page-1)*$limit;
		if(isset($name_link) && $name_link != null)
		{	
			$id_utilities	= $this->stores_model->get_utilities_info_by_name_link($name_link);
			$result			=	$this->stores_model->get_all_products_by_utilities($id_utilities->id,$limit,$offset);
		}
		$num_rows   =	$result['num_rows'];
		$rows		=	$result['rows'];
		$total		=	ceil($num_rows/$limit);			
		
		$menu 			= trim($this->uri->segment(3));
		$menu_current 	= str_replace('.html','',$menu);
		
		$url			=	base_url().'gian-hang/su-dung/'.$menu_current;		
			
		$conf = array(
			'cur_page'			=>	$page,
			'base_url'			=>	$url,
			'total_rows'		=>	$total,
			'cur_page'			=>	$page,			
			'cur_class'			=>	'currentpage',
			'prev_class'		=>	'prevnext',
			'next_class'		=>	'prevnext',
			'next_link'			=>	'Tiếp theo',
			'prev_link'			=>	'Trang trước',
			'show_total'		=>	'no',
			'show_first_last'	=>	'no'
		);
		
		$this->pagination->initialize($conf);
		$this->data['pagnav'] 		= $this->pagination->non_ajax();
		$this->data['page']			= $page;
		$this->data['num_rows'] 	= $num_rows;
		$this->data['total_page'] 	= $total;
		
		$this->data['all_products']	= $rows;
		
		//meta tag
	    $this->data['title'] 			= get_title('[TITLE]',"Gian hàng");
	    $this->data['description'] 		= get_description('[DESCRIPTION]',"Gian hàng");
		$this->data['keywords'] 		= get_keywords('[KEYWORDS]',"Gian hàng");	    
		//end meta tag
									
		$this->data['tpl_file']		=	'stores/index';
		$this->load->view('layout/default',$this->data);
	}
	
	function filter_power($power = null,$page = 1)
	{
		$limit		=	8;
		$offset     = 	($page-1)*$limit;
		if(isset($power) && $power != null)
		{
			$result		=	$this->stores_model->get_all_products_by_power($power,$limit,$offset);
		}
		$num_rows   =	$result['num_rows'];
		$rows		=	$result['rows'];
		$total		=	ceil($num_rows/$limit);			
		
		$menu 			= trim($this->uri->segment(3));
		$menu_current 	= str_replace('.html','',$menu);
		$url			=	base_url().'gian-hang/power/'.$menu_current;		
			
		$conf = array(
			'cur_page'			=>	$page,
			'base_url'			=>	$url,
			'total_rows'		=>	$total,
			'cur_page'			=>	$page,			
			'cur_class'			=>	'currentpage',
			'prev_class'		=>	'prevnext',
			'next_class'		=>	'prevnext',
			'next_link'			=>	'Tiếp theo',
			'prev_link'			=>	'Trang trước',
			'show_total'		=>	'no',
			'show_first_last'	=>	'no'
		);
		
		$this->pagination->initialize($conf);
		$this->data['pagnav'] 		= $this->pagination->non_ajax();
		$this->data['page']			= $page;
		$this->data['num_rows'] 	= $num_rows;
		$this->data['total_page'] 	= $total;
		
		$this->data['all_products']	= $rows;
		
		//meta tag
	    $this->data['title'] 			= get_title('[TITLE]',"Gian hàng");
	    $this->data['description'] 		= get_description('[DESCRIPTION]',"Gian hàng");
		$this->data['keywords'] 		= get_keywords('[KEYWORDS]',"Gian hàng");	    
		//end meta tag
									
		$this->data['tpl_file']		=	'stores/index';
		$this->load->view('layout/default',$this->data);
	}
	
	function search($keywords = null,$page = 1)
	{
		$limit		=	8;
		$offset     = 	($page-1)*$limit;
		if(isset($keywords) && $keywords != null)
		{
			$keywords = str_replace('+',' ',$keywords);
			$keywords = str_replace('_',' ',$keywords);
			$keywords = preg_replace('/[\:\!\?\&\<\>\#\'\"]+/is','',$keywords);
		
			$result		=	$this->stores_model->search_products_by_keywords($keywords,$limit,$offset);
		}
		$num_rows   =	$result['num_rows'];
		$rows		=	$result['rows'];
		$total		=	ceil($num_rows/$limit);			
		
		$menu 			= trim($this->uri->segment(3));
		$menu_current 	= str_replace('.html','',$menu);
		$url			=	base_url().'gian-hang/search-products/'.$menu_current;		
			
		$conf = array(
			'cur_page'			=>	$page,
			'base_url'			=>	$url,
			'total_rows'		=>	$total,
			'cur_page'			=>	$page,			
			'cur_class'			=>	'currentpage',
			'prev_class'		=>	'prevnext',
			'next_class'		=>	'prevnext',
			'next_link'			=>	'Tiếp theo',
			'prev_link'			=>	'Trang trước',
			'show_total'		=>	'no',
			'show_first_last'	=>	'no'
		);
		
		$this->pagination->initialize($conf);
		$this->data['pagnav'] 		= $this->pagination->non_ajax();
		$this->data['page']			= $page;
		$this->data['num_rows'] 	= $num_rows;
		$this->data['total_page'] 	= $total;
		
		$this->data['all_products']	= $rows;
		
		//meta tag
	    $this->data['title'] 			= get_title('[TITLE]',"Gian hàng");
	    $this->data['description'] 		= get_description('[DESCRIPTION]',"Gian hàng");
		$this->data['keywords'] 		= get_keywords('[KEYWORDS]',"Gian hàng");	    
		//end meta tag
									
		$this->data['tpl_file']		=	'stores/index';
		$this->load->view('layout/default',$this->data);
	}
	
	function detail($id_products = null,$name_link = null)
	{
		$products_info 	= $this->stores_model->get_products_info_by_id($id_products);
		$this->data['products_info'] 	= $products_info; 
		$this->data['products_orther']	= $this->stores_model->get_products_orther_power_cool($products_info[0]->cool_power,$id_products);
		
		$this->data['question_pro']		= $this->stores_model->get_questions_with_products($id_products);
		
		$this->data['articles_pro']		= $this->stores_model->get_articles_with_products($id_products);
		
		//meta tag
	    $this->data['title'] 			= get_title('[TITLE]',$products_info!= null ? $products_info[0]->name : "");
	    $this->data['description'] 		= get_description('[DESCRIPTION]',$products_info!= null ? $products_info[0]->name : "");
		$this->data['keywords'] 		= get_keywords('[KEYWORDS]',$products_info!= null ? $products_info[0]->name : "");	    
		//end meta tag
		
		$this->data['tpl_file']		=	'stores/detail';
		$this->load->view('layout/default',$this->data);
	}
}
